import createElement from './createElement.js'
export default function patchNode(oldVnode, newVnode) {
    console.log("是同一个节点333")
    // 判断新旧vnode是否是同一个对象
    if (oldVnode === newVnode) return
    // 判断新vnode有没有text属性
    if (newVnode.text !== undefined && (newVnode.children === undefined || newVnode.children.length === 0)) {
        if (newVnode.text !== oldVnode.text) {
            oldVnode.elm.innerText = newVnode.text
        }
    } else {
        console.log("走到着了吗", oldVnode)
        if (oldVnode.children !== undefined && oldVnode.children.length > 0) {
            console.log(555)
            let un = 0
            // 如果oldVnode有children
            for (let i = 0; i < newVnode.children.length; i++) {
                let ch = newVnode.children[i]
                // 再次遍历。看看oldVnode中有没有和新节点是same的
                let isExist = false
                for (let j = 0; j < oldVnode.children.length; j++) {
                    if (oldVnode.children[j].sel === ch.sel && oldVnode.children[j].key === ch.key) {
                        isExist = true
                    }
                }
                if (!isExist) {
                    let dom = createElement(ch)
                    ch.elm = dom
                    if (un < oldVnode.children.length) {
                        oldVnode.elm.insertBefore(dom, oldVnode.children[un].elm)
                    } else {
                        oldVnode.elm.appendChild(dom, oldVnode.children[un].elm)
                    }
                } else {
                    un++
                }
                
            }
        } else {
            oldVnode.elm.innerHTML = ""
            for (let i = 0; i < newVnode.children.length; i++) {
                let dom = createElement(newVnode.children[i])
                oldVnode.elm.appendChild(dom)
            }
        }
    }
}